<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item label="激活码">
              <a-input placeholder="请输入激活码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="7" :lg="8" :md="9" :sm="24">
            <a-form-item label="对应题库分类">
              <ao-category-select placeholder="请选择对应题库分类" v-model="queryParam.categoryId" returnColumn="code"  pcode=""/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="所属用户">
                <j-select-user-by-dep placeholder="请选择所属用户" v-model="queryParam.owner"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="激活人">
                <a-input placeholder="请输入激活人" v-model="queryParam.activeUser"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="激活时间">
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.activeTime_begin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.activeTime_end"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="cdkey_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'cdkey:add'" @click="handleAdd" type="primary" icon="plus">批量生成</a-button>
      <a-button v-has="'cdkey:export'" type="primary" icon="download" @click="handleExportXls('兑换码')">导出</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'cdkey:del'"><a-icon type="delete"/>删除</a-menu-item>
          <a-menu-item key="2" @click="batchAssign" v-has="'cdkey:assign'"><a-icon type="api"/>分配</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider v-has="'cdkey:assign'" type="vertical" />
          <a v-has="'cdkey:assign'" @click="assign(record)">分配</a>
          <a-divider v-has="'cdkey:del'" type="vertical" />
          <a-popconfirm v-has="'cdkey:del'" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    
    <ao-cdkey-modal ref="detailModal"></ao-cdkey-modal>

    <ao-cdkey-add-modal ref="modalForm" @ok="modalFormOk"></ao-cdkey-add-modal>
    
    <ao-cdkey-assign-modal ref="assignForm" @ok="modalFormOk"></ao-cdkey-assign-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import AoCdkeyModal from './modules/AoCdkeyModal'
  import AoCdkeyAddModal from './modules/AoCdkeyAddModal'
  import AoCdkeyAssignModal from './modules/AoCdkeyAssignModal'
  import { loadAoCategoryData } from '@/api/api'

  export default {
    name: 'AoCdkeyList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      AoCdkeyModal,
      AoCdkeyAddModal,
      AoCdkeyAssignModal
    },
    data () {
      return {
        description: '兑换码管理页面',
        // 表头
        columns: [
          /* {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          }, */
          {
            title:'激活码',
            align:"center",
            dataIndex: 'code',
            customRender:function (text) {
              return text.substr(0,26) + "******";
            }
          },
          {
            title:'题库分类',
            align:"center",
            dataIndex: 'categoryId_dictText',
          },
          {
            title:'所属用户',
            align:"center",
            dataIndex: 'owner_dictText'
          },
          {
            title:'激活人',
            align:"center",
            dataIndex: 'activeUser'
          },
          {
            title:'激活时间',
            align:"center",
            sorter: true,
            dataIndex: 'activeTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'状态',
            align:"center",
            sorter: true,
            dataIndex: 'status_dictText'
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/cdkey/aoCdkey/list",
          delete: "/cdkey/aoCdkey/delete",
          deleteBatch: "/cdkey/aoCdkey/deleteBatch",
          exportXlsUrl: "/cdkey/aoCdkey/exportXls",
          
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    methods: {
      handleDetail(record){
        this.$refs.detailModal.edit(record);
        this.$refs.detailModal.title="详情";
        this.$refs.detailModal.disableSubmit = true;
      },
      assign(record){
        this.$refs.assignForm.show(record.id)
      },
      batchAssign(){
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
        this.$refs.assignForm.show(ids);
        }
      },
      initDictConfig(){
        loadAoCategoryData({code: '0'}).then((res) => {
          if (res.success) {
            this.$set(this.dictOptions, 'categoryId', res.result)
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
        fieldList.push({type:'datetime',value:'createTime',text:'创建日期'})
        fieldList.push({type:'string',value:'code',text:'激活码',dictCode:''})
        fieldList.push({type:'string',value:'categoryId',text:'对应题库分类'})
        fieldList.push({type:'sel_user',value:'owner',text:'所属用户'})
        fieldList.push({type:'string',value:'activeUser',text:'激活人',dictCode:''})
        fieldList.push({type:'date',value:'activeTime',text:'激活时间'})
        fieldList.push({type:'string',value:'status',text:'状态',dictCode:'cdkey_status'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>